#ifndef OTEXTURE_H
#define OTEXTURE_H

#include "OString.h"
#include "png/png.hpp"
#if defined(__APPLE__) || defined(MACOSX)
	   #include <OpenGL/OpenGL.h>
#endif
#if defined(_WIN32)
	#include <GL/gl.h>
#endif

namespace Y{

class Texture{
	
	public:
		GLubyte *image_data;
		bool loaded;
		OString path;
		int width;
		int height;
		int fourHeight;
		int yID;
		GLuint texID;
		
		static bool use_mipmaps_default;
		
		static Texture* protoLoadedTexture ( const char * pt );
		static Texture* mipmappedTexture ( const char * pt );
		
	public:
		
		Texture (): image_data(NULL),loaded(false),width(0),height(0),fourHeight(0),texID(0){ _init(); }
		Texture ( const char * path, bool to_texture = true, bool keep = false );
		Texture ( int w, int h );
		Texture ( const Texture& t );
		Texture& operator= ( const Texture& t );
			void _init();
		~Texture (){ if ( image_data != NULL ){ delete[] image_data; } }
		
		void load ();
		void load (const char *path);
		void clearData () { if ( image_data != NULL ){ delete[] image_data; }image_data = NULL;loaded = false; }
		
		GLuint toGLTexture ( bool mipmap = false, bool keep = false );
		
		GLubyte& operator() (int i, int j, int rgba){ return (image_data[i * fourHeight + j * 4 + rgba]); }
		GLubyte& acc (int i, int j, int rgba);
		
		void bind () { glBindTexture(GL_TEXTURE_2D,texID); }
		static void unbind () { glBindTexture(GL_TEXTURE_2D,0); }
};


}
#endif
